package com.cskaoyan.order.biz.handler;

import com.cskaoyan.order.biz.context.CreateOrderContext;
import com.cskaoyan.order.biz.context.TransHandlerContext;
import com.cskaoyan.order.mq.producer.DelyOrderCancelProducer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @Description: 利用mq发送延迟取消订单消息
 **/
@Component
@Slf4j
public class SendMessageHandler extends AbstractTransHandler {

    // 注入我们自己定义的Producer对象
    @Autowired
    private DelyOrderCancelProducer producer;

    @Override
    public boolean isAsync() {
        return false;
    }

    @Override
    public boolean handle(TransHandlerContext context) {
        // 当代码执行到这里，下单已经成功了，针对已经下单成功的订单，发送延迟消息(延迟时间就是订单的超时时间)
        CreateOrderContext createOrderContext = (CreateOrderContext) context;
        // 从上下文对象中获取订单Id
        String orderId = createOrderContext.getOrderId();
        // 将orderId,放入延迟消息中发送(调用我们自己定义的producer对象)
        return producer.sendOrderCancelMessage(orderId);
    }
}